package com.yayohos.locationmanager;


import com.yayohos.locationmanager.constants.FailType;
import com.yayohos.locationmanager.constants.ProcessType;
import com.yayohos.locationmanager.helper.LogUtils;

import ohos.agp.utils.TextTool;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.location.Location;

/**
 * 定位数据处理
 */
public class SamplePresenter {
    private SampleView sampleView;

    public SamplePresenter(SampleView view) {
        this.sampleView = view;
    }

    /**
     * 销毁
     */
    public void destroy() {
        sampleView = null;
    }

    /**
     * 定位成功
     *
     * @param location 定位成功后获取的坐标
     */
    public void onLocationChanged(Location location) {
        sampleView.dismissProgress();
        setText(location);
    }

    /**
     * 定位失败
     *
     * @param failType 定位失败的原因类型
     */
    public void onLocationFailed(int failType) {
        sampleView.dismissProgress();
        switch (failType) {
            case FailType.TIMEOUT:
                toMainThread("Couldn't get location, and timeout!");
                break;
            case FailType.PERMISSION_DENIED:
                toMainThread("Couldn't get location, because user didn't give permission!");
                break;
            case FailType.NETWORK_NOT_AVAILABLE:
                toMainThread("Couldn't get location, because network is not accessible!");
                break;
            case FailType.GPS_NOT_OPEN:
                toMainThread("Couldn't get location, because GPS is not enable，please turn it on!");
                break;
            case FailType.VIEW_DETACHED:
                sampleView.setText("Couldn't get location, because in the process view was detached!");
                break;
            case FailType.VIEW_NOT_REQUIRED_TYPE:
                sampleView.setText("Couldn't get location, "
                        + "because view wasn't sufficient enough to fulfill given configuration!");
                break;
            case FailType.UNKNOWN:
                sampleView.setText("Ops! Something went wrong!");
                break;
        }
    }

    /**
     * 定位进度回调
     *
     * @param newProcess 定位新进度
     */
    public void onProcessTypeChanged(int newProcess) {
        switch (newProcess) {
            case ProcessType.GETTING_LOCATION_FROM_GPS_PROVIDER:
                sampleView.updateProgress("Getting Location from GPS...");
                break;

            case ProcessType.GETTING_LOCATION_FROM_NETWORK_PROVIDER:
                sampleView.updateProgress("Getting Location from Network...");
                break;

            case ProcessType.ASKING_PERMISSIONS:
                sampleView.updateProgress("ASKING_PERMISSIONS...");
                break;
            case ProcessType.GETTING_LOCATION_FROM_CUSTOM_PROVIDER:
                // Ignored
                break;
            default:
                break;
        }
    }

    private void setText(Location location) {
        String appendValue =
                System.getProperty("line.separator") + location.getLatitude() + ", " + location.getLongitude();
        String newValue;
        CharSequence current = sampleView.getText();
        LogUtils.logI(appendValue + "/" + current);

        if (!TextTool.isNullOrEmpty(current)) {
            newValue = current + appendValue;
        } else {
            newValue = appendValue;
        }
        toMainThread(newValue);
    }

    private void toMainThread(String value) {
        EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());
        handler.postTask(new Runnable() {
            @Override
            public void run() {
                sampleView.setText(value);
            }
        });
    }

    /**
     * 数据处理的对外接口
     */
    public interface SampleView {
        /**
         * 获取文本已显示的文字
         *
         * @return String 当前Text的文本
         */
        String getText();

        /**
         * 配置文本
         *
         * @param text 根据定位回调设置的文本
         */
        void setText(String text);

        /**
         * 更新定位进度
         *
         * @param text 定位进度
         */
        void updateProgress(String text);

        /**
         * 隐藏定位进度
         */
        void dismissProgress();

    }

}
